package com.chukun.netty.protobuf;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * @author chukun
 * 定义protobuf服务端的处理器
 */
public class NettyProtobufServerHandler extends SimpleChannelInboundHandler<MessageData.MessageInfo> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, MessageData.MessageInfo messageInfo) throws Exception {
        MessageData.MessageInfo.DataType dataType = messageInfo.getDataType();
        if(dataType==MessageData.MessageInfo.DataType.PersonType){
            //处理普通person的数据类型
            MessageData.Person person = messageInfo.getPerson();
            System.out.println("--------------person信息-----------------");
            System.out.println("name= "+person.getName()+" , age= "+person.getAge());
        }else if(dataType==MessageData.MessageInfo.DataType.TeacherType){
            //处理teacher的数据类型
            MessageData.Teacher teacher = messageInfo.getTeacher();
            System.out.println("--------------teacher信息-----------------");
            System.out.println("name= "+teacher.getName()+" , age= "+teacher.getAge()+" , course= "+teacher.getCourse());
            System.out.println("--------------住址信息---------------------");
            MessageData.Address address = teacher.getAddress();
            System.out.println("省份编码: "+address.getProvinceCode()+" 市级编码 : "+address.getCityCode()+", 区域编码: "+address.getAreaCode());
        }else {
            //处理Driver的数据类型
            //处理teacher的数据类型
            MessageData.Driver driver = messageInfo.getDriver();
            System.out.println("--------------driver信息-----------------");
            System.out.println("name= "+driver.getName()+" , age= "+driver.getAge()+" , diver bus= "+driver.getDriverBus());
            System.out.println("--------------driver住址信息---------------------");
            MessageData.Address address = driver.getAddress();
            System.out.println("省份编码: "+address.getProvinceCode()+" 市级编码 : "+address.getCityCode()+", 区域编码: "+address.getAreaCode());
        }
    }
}
